package org.jeecg.modules.zcgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.zcgl.entity.*;
import org.jeecg.modules.zcgl.enums.HandleTypeEnum;
import org.jeecg.modules.zcgl.enums.PayStatusEnum;
import org.jeecg.modules.zcgl.enums.PaymentInstructionsEnum;
import org.jeecg.modules.zcgl.mapper.CustomerInfoMapper;
import org.jeecg.modules.zcgl.service.ICustomerBalanceService;
import org.jeecg.modules.zcgl.service.ICustomerInfoService;
import org.jeecg.modules.zcgl.service.IPayablesInfoService;
import org.jeecg.modules.zcgl.util.NoUtils;
import org.jeecg.modules.zcgl.vo.statistical.CustomerPaymentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 客户管理
 * @Author: jeecg-boot
 * @Date:   2020-06-17
 * @Version: V1.0
 */
@Service
@Transactional
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {

    @Autowired
    private ICustomerBalanceService customerBalanceService;
    @Autowired
    private IPayablesInfoService payablesInfoService;
//    @Autowired
//    private IPayablesDetailsService payablesDetailsService;

    @Override
    public void balanceRefundCheck(CustomerBalanceChangesDetail vo) {
        if (vo.getChangesAmount() == null || vo.getChangesAmount().compareTo(BigDecimal.ZERO) <1){
            throw new JeecgBootException("退款金额必须大于0！");
        }
        LoginUser sysUser = JwtUtil.getLoginUser();
        CustomerBalance balance = customerBalanceService.selectCustomerIdAndCompanyId(sysUser.getCurrentCompanyId(), vo.getCustomerId());
        if (balance == null || balance.getBalance() == null){
            throw new JeecgBootException("退款失败，没有预存款或信息错误！");
        }
        if (balance.getBalance().compareTo(vo.getChangesAmount()) < 0){
            //保留两位小数并展示千分位符
            DecimalFormat df1 = new DecimalFormat("##,##0.00");
            throw new JeecgBootException("退款失败，余额不足，当前余额：" + df1.format(balance.getBalance()));
        }
    }

    @Override
    @Transactional
    public boolean balanceRefund(CustomerBalanceChangesDetail vo) {
        this.balanceRefundCheck(vo);
        //生成付款记录
        PayablesInfo payablesInfo = new PayablesInfo();
        payablesInfo.setPayablesNo(NoUtils.getPayablesNo());
        payablesInfo.setPayStatus(PayStatusEnum.UNPAID.getStatus());
        payablesInfo.setCustomerId(vo.getCustomerId());
        payablesInfo.setTotalMoney(vo.getChangesAmount());
        payablesInfo.setRemark(vo.getRemark() + "（预存款退款）");

        PayablesDetails details = new PayablesDetails();
        details.setAmountMoney(vo.getChangesAmount());
        details.setCustomerId(vo.getCustomerId());
        details.setPaymentInstructions(PaymentInstructionsEnum.BALANCE.getStatus());

        List<PayablesDetails> detailsList = new ArrayList<>();
        detailsList.add(details);
        payablesInfoService.saveMain(payablesInfo, detailsList);

        return customerBalanceService.updateBalanceByCustomerIdAndCompanyId(vo.getCompanyId(),vo.getCustomerId(),
                vo.getChangesAmount(), false, payablesInfo.getId(), HandleTypeEnum.BALANCE_REFUND.getStatus(), vo.getDescription(),null);
    }

    @Override
    public IPage<CustomerPaymentVo> queryCustomerPaymentVoList(Page<CustomerPaymentVo> page, QueryWrapper<CustomerPaymentVo> queryWrapper) {
        return baseMapper.queryCustomerPaymentVoList(page,queryWrapper);
    }

    @Override
    public List<CustomerPaymentVo> queryCustomerPaymentVoList(QueryWrapper<CustomerPaymentVo> queryWrapper) {
        return baseMapper.queryCustomerPaymentVoList(queryWrapper);
    }
}
